@echo off
chcp 65001 >nul
echo ========================================
echo RuoYi System Docker Build Script
echo ========================================

echo 1. Cleaning Maven project...
call mvn clean
if %errorlevel% neq 0 (
    echo Maven clean failed!
    pause
    exit /b 1
)

echo 2. Compiling and packaging project...
call mvn package -Dmaven.test.skip=true
if %errorlevel% neq 0 (
    echo Maven package failed!
    pause
    exit /b 1
)

echo 3. Checking if JAR file exists...
if not exist "ruoyi-admin\target\ruoyi-admin.jar" (
    echo JAR file does not exist, please check if compilation was successful!
    pause
    exit /b 1
)

echo 4. Building Docker image...
rem Generate timestamp using PowerShell for better compatibility
for /f "usebackq delims=" %%i in (`powershell -Command "Get-Date -Format 'yyyyMMdd-HHmmss'"`) do set "datestamp=%%i"
echo Building image with tag: ruoyi:%datestamp%
docker build -t ruoyi:%datestamp% .
if %errorlevel% neq 0 (
    echo Docker image build failed!
    pause
    exit /b 1
)
echo Also tagging as latest...
docker tag ruoyi:%datestamp% ruoyi:latest

echo 5. Displaying built images...
docker images | findstr ruoyi

echo ========================================
echo Build completed!
echo Image built with timestamp tag: ruoyi:%datestamp%
echo Also tagged as: ruoyi:latest
echo Use the following command to start services:
echo docker-compose up -d
echo ========================================
pause